"""爬取在线书店http://books.toscrape.com/的所有信息，包括图书的名字、封面图片、价格、评分、库存、产品介绍、所属分类。"""


import  requests
from bs4 import BeautifulSoup
import time
import random
from lxml import etree
import re

start_url = 'http://books.toscrape.com'
next_page_url = start_url



#获取图书URL内容
def get_info(url):
    r2 = requests.get(url)
    r2.raise_for_status()
    bs2 = BeautifulSoup(r2.text, 'html.parser')
    #书名
    book_name = bs2.find('h1')
    print('book name: ', book_name.text)
    #价格
    price = bs2.find('p', class_ ='price_color')
    print('price: ', price.text[1:])
    #剩余量
    availability = bs2.find('p', class_='instock availability')
    print('availability: ', availability.text.strip())
    #正则匹配得分 读取r2.text 在html 文本中使用正则
    score = re.findall('class="star-rating (.*?)">', r2.text)
    print("score: ", score[0])
    #匹配描述   由于r2.text是带换行符的, 所以先去掉换行符
    description = re.findall('</h2>\s{8}</div>\s{8}<p>(.*?)</p>', ''.join(''.join(r2.text.split('\n'))))
    print("description: ", description[0])
    r2.close()
    time.sleep(2)

while next_page_url:
    r = requests.get(start_url)
    r.raise_for_status()

    bs = BeautifulSoup(r.text, 'html.parser')
    li_list = bs.find_all('article', class_= 'product_pod')
    for li in li_list:
        book_url  = start_url + '/' + li.a['href']
        #书籍链接
        print('book_link: ' ,book_url)
        picture_link = start_url + '/' + li.img['src']
        #图片链接
        print('picture_link', picture_link)
        #打开书籍链接, 获取详细信息
        get_info(book_url)

    # 查找下一页链接
    next_page = bs.find('li', class_ = 'next')
    next_page_url  =next_page.a['href']
    print(type(next_page))
    # 合成下一页链接
    next_page_url = start_url +'/' +next_page_url
    if not next_page:
        break


